package com.clas.liu.myapplication.view.activity.impl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.notice.Notice;
import com.clas.liu.myapplication.model.entity.notice.NoticeData;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;
import com.clas.liu.myapplication.presenter.impl.NoticePresenter;
import com.clas.liu.myapplication.view.activity.i.INoticeActivity;
import com.clas.liu.myapplication.view.adapter.NoticeAdapter;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.util.SaveUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2019/5/13.
 */

public class NoticeActivity extends BaseActivity<NoticePresenter> implements INoticeActivity,View.OnClickListener{

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private ImageView back;
    private NoticeAdapter adapter;
    private LinearLayoutManager layoutManager;
    private int page_no = 1;
    private final int page_size = 10;
    private List<Notice> infos;
    private boolean isRefresh = true;
    private boolean isAll = false;
    private int lastVisibleItem;
    private String categoryStr;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_layout);
        findView();
        init();
        setListener();
    }

    private void init() {
        refreshLayout.setRefreshing(true);
        mPresenter.getNotices(page_no, page_size);

    }

    private void setListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                infoRefresh();
            }
        });
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (adapter != null && newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount()) {
                    if (!isAll)
                        mPresenter.getNotices( page_no, page_size);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
        back.setOnClickListener(this);
    }

    private void findView() {
        recyclerView = findViewById(R.id.notice_rv);
        refreshLayout = findViewById(R.id.notice_refresh);
        back = findViewById(R.id.notice_back_bt);
    }

    public void infoRefresh() {
        if (NetworkHelper.isNetworkAvailable(this)) {
            page_no = 1;
            isRefresh = true;
            mPresenter.getNotices(page_no, page_size);
        } else {
            refreshLayout.setRefreshing(false);
            showToast("亲亲，网好像断了呢!~", Toast.LENGTH_SHORT);
        }
    }

    @Override
    protected NoticePresenter getPresenter() {
        return new NoticePresenter(this);
    }

    @Override
    public void failBecauseNetReturn(int code) {
        showToast(code+"请求失败！", Toast.LENGTH_SHORT);
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void getNoticeReturn(NoticeData data) {
        SaveUtil.saveNoticeCount(getApplicationContext(),data.getTotal());
        if (infos == null) {
            infos = new ArrayList<>();
        }
        if (isRefresh) {
            infos.clear();
            isRefresh = false;
        }
        infos.addAll(data.getData());
        if (isRefresh) {
            if (adapter != null)
                adapter.deleteLoadingMore(false);
        }
        recyclerView.setVisibility(View.VISIBLE);
        refreshLayout.setRefreshing(false);
        if (layoutManager == null) {
            layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
        }
        if (adapter == null) {
            adapter = new NoticeAdapter(infos,this);
            recyclerView.setAdapter(adapter);
        } else
            adapter.update(infos);
        if (data.getPageNum() == data.getPages()) {
            adapter.deleteLoadingMore(true);
            isAll = true;
        } else if (data.getPageNum() < data.getPages()) {
            page_no++;
            isAll = false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.notice_back_bt:
                finish();
                break;
        }
    }
}
